package com.lh.controller;

import com.lh.entity.Category;
import com.lh.service.CategoryService;
import com.mysql.jdbc.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: LuoHang
 * @Date: 2021/2/11 12:25
 * @Description
 */
@Controller
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 后台管理之商品分类列表页面
     *
     * @return 视图模型
     */
    @RequestMapping("/categoryListHtml")
    public ModelAndView categoryListHtml() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("admin/category_list");
        return modelAndView;
    }

    /**
     * 后台管理之分类列表数据展示——Lay_ui表格
     *
     * @param request 请求对象
     * @param limit   每页显示的行数
     * @param page    当前第几页
     * @return Lay_ui数据表格需要的数据
     */
    @RequestMapping("/getCategoryList")
    @ResponseBody
    public Map<String, Object> getCategoryList(HttpServletRequest request, @RequestParam Integer limit, @RequestParam Integer page) {
        String id = request.getParameter("id");
        String categoryName = request.getParameter("categoryName");
        Map<String, Object> query = new HashMap<>(5);
        if (!StringUtils.isNullOrEmpty(id)) {
            query.put("categoryId", Integer.valueOf(id));
        }
        if (!StringUtils.isNullOrEmpty(categoryName)) {
            query.put("categoryName", categoryName);
        }
        query.put("beginSize", (page - 1) * limit);
        query.put("limit", limit);

        //lay-ui需要的数据
        List<Category> categoryList = categoryService.getCategoryList(query);
        Map<String, Object> map = new HashMap<>(4);
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", categoryService.getCount(query));
        map.put("data", categoryList);
        return map;
    }

    /**
     * 后台分类列表之添加分类功能
     *
     * @param categoryName 分类名
     * @param categoryDesc 分类描述
     * @return 是否添加成功
     */
    @RequestMapping("/addCategory")
    @ResponseBody
    public Map<String, Object> addCategory(@RequestParam String categoryName, @RequestParam String categoryDesc) {
        Category category = new Category(null, categoryName, categoryDesc, null);
        int count = categoryService.addCategory(category);
        Map<String, Object> res = new HashMap<>(1);
        res.put("data", count > 0 ? "添加成功" : "添加失败");
        return res;
    }

    @RequestMapping("/addCategoryHtml")
    public ModelAndView addCategoryHtml() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("admin/category_add");
        return modelAndView;
    }

    /**
     * 后台分类列表之删除指定id分类功能
     *
     * @param id 对应的id
     * @return 是否删除成功
     */
    @RequestMapping("/deleteCategoryById")
    @ResponseBody
    public Map<String, Object> deleteCategoryById(@RequestParam Integer id) {
        int count = categoryService.deleteCategoryById(id);
        Map<String, Object> res = new HashMap<>(1);
        res.put("data", count > 0 ? "删除成功" : "删除失败");
        return res;
    }

    /**
     * 后台分类列表之更新分类信息功能
     *
     * @param categoryName 分类名
     * @param categoryDesc 分类描述
     * @return 是否更新成功
     */
    @RequestMapping("/updateCategory")
    @ResponseBody
    public Map<String, Object> updateCategory(@RequestParam Integer id, @RequestParam String categoryName, @RequestParam String categoryDesc) {
        Map<String, Object> updateInfo = new HashMap<>(3);
        updateInfo.put("gmtModified", new Timestamp(System.currentTimeMillis()));
        updateInfo.put("id",id);
        Category category = categoryService.getCategoryById(id);
        if (!StringUtils.isNullOrEmpty(categoryName) && !categoryName.equals(category.getCategoryName())) {
            updateInfo.put("categoryName", categoryName);
        }
        if (!StringUtils.isNullOrEmpty(categoryDesc) && !categoryDesc.equals(category.getCategoryDesc())) {
            updateInfo.put("categoryDesc", categoryDesc);
        }
        int count = categoryService.updateCategory(updateInfo);

        Map<String, Object> res = new HashMap<>(1);
        res.put("data", count > 0 ? "修改成功" : "修改失败");
        return res;
    }

    /**
     * 更新页面
     *
     * @param id 对应id
     * @return 视图模型
     */
    @RequestMapping("/updateCategoryHtml")
    public ModelAndView updateCategoryHtml(@RequestParam Integer id) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("admin/category_edit");
        modelAndView.addObject("categoryInfo", categoryService.getCategoryById(id));
        return modelAndView;
    }
}
